package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.vo.ViewMember;
import com.zzxz.practicaltrainingsys.entity.Member;
import com.zzxz.practicaltrainingsys.entity.Student;
import com.zzxz.practicaltrainingsys.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    // 插入成员信息（member表），插入小组组号、组员学号
    // POST http://localhost:8080/member/insertmember
    @PostMapping("/insertmember")
    public Map<String, Object> insertMember(@RequestBody Member member) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 校验该小队人数是否已满
            int memberCount = memberService.listByTeamId(member.getTeam_id()).size();
            if (memberCount >= 6) {
                result.put("success", false);
                result.put("msg", "该小队人数已满，无法加入！");
                return result;
            }
            int existCount = memberService.checkMemberExist(member.getTeam_id(), member.getStudent_id());
            if (existCount > 0) {
                result.put("success", false);
                result.put("msg", "您已经加入该队伍");
                return result;
            }

            int insertResult = memberService.insertMember(member);
            if (insertResult == 1) {
                result.put("success", true);
                result.put("msg", "加入成功");
            } else {
                result.put("success", false);
                result.put("msg", "加入失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "服务器错误: " + e.getMessage());
        }
        return result;
    }

    // 根据小组号(team_id)在member表查找组员学号
    // GET http://localhost:8080/member/group/{team_id}/ids
    @GetMapping("/group/{team_id}/ids")
    public List<String> getStudentIdsByTeamId(@PathVariable Integer team_id) {
        return memberService.getStudentIdsByGroupId(team_id);
    }

    // 根据小组号(team_id)查取组员信息
    // GET http://localhost:8080/member/group/{team_id}/members
    @GetMapping("/group/{team_id}/members")
    public List<Student> getStudents(@PathVariable Integer team_id) {
        return memberService.getStudents(team_id);
    }

    // 根据组员学号在member表插入组员分数
    // POST http://localhost:8080/member/updatescore
    @PostMapping("/updatescore")
    public int updateStudentScore(@RequestBody Member member) {
        return memberService.updateStudentScore(member);
    }

    // 根据学号在member表查询个人成绩
    // GET http://localhost:8080/member/score/{student_id}
    @GetMapping("/score")
    public Map<String, Object> getScoreById(@RequestParam("student_id") String student_id) {
        Integer memberMarks = memberService.getScoreById(student_id);
        Map<String, Object> response = new HashMap<>();
        response.put("member_marks", memberMarks != null ? memberMarks : "暂无成绩");
        return response;
    }

    // 根据学生ID查询其所有小组成员信息（判断是否已加入任何小队）
    @GetMapping("/findByStudentId")
    public List<Member> findByStudentId(@RequestParam("student_id") Integer student_id) {
        return memberService.findByStudentId(student_id);
    }

    // 根据小组ID查询所有成员成绩
    @GetMapping("/listByTeamId")
    public List<Member> listByTeamId(@RequestParam("team_id") Integer team_id) {
        return memberService.listByTeamId(team_id);
    }

    // MemberController.java
    @GetMapping("/checkInProjectTeam")
    public Map<String, Object> checkInProjectTeam(@RequestParam Integer student_id, @RequestParam Integer project_id) {
        Map<String, Object> result = new HashMap<>();
        com.zzxz.practicaltrainingsys.vo.ProjectTeamInfo info = memberService.getProjectTeamInfo(student_id, project_id);
        if (info != null) {
            result.put("inTeam", true);
            result.put("leader_id", info.getLeader_id());
            result.put("team_id", info.getTeam_id());
            result.put("team_name", info.getTeam_name());
            result.put("topic_name", info.getTopic_name());
            result.put("topic_id", info.getTopic_id());
            result.put("invite_code", info.getInvite_code());
        } else {
            result.put("inTeam", false);
        }
        return result;
    }

    @GetMapping("/listMemberInfoByTeamId")
    public List<ViewMember> listMemberInfoByTeamId(@RequestParam("team_id") Integer team_id) {
        return memberService.listMemberInfoByTeamId(team_id);
    }


}